#include "widget.h"
#include "ui_widget.h"
#include "pack.h"
#include "QPushButton"

Widget::Widget(QWidget *parent)
    : QWidget(parent),
      user_interface(nullptr),
      ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("client");
    client = new QTcpSocket(this);
    client->connectToHost(QHostAddress::LocalHost, 8089);

    qDebug() << "connect success" ;

    QObject::connect(client, &QTcpSocket::readyRead, this, &Widget::onReadyRead);

}

Widget::~Widget()
{
    if(user_interface != nullptr){
        delete user_interface;
    }
    delete ui;
}

bool Widget::regist_back(Pack pack)
{
    if(pack.getErr() == SUCCESS){
        QMessageBox::information(this, "regist", "Regist success");
        return true;
    }else{
        QMessageBox::warning(this, "regist", "This account already exists ");
        client->disconnectFromHost();
        return false;
    }
}

bool Widget::login_back(Pack pack)
{
    if(pack.getErr() == SUCCESS){
        QMessageBox::information(this, "login", "login success");
        return true;
    }else{
        QMessageBox::warning(this, "login", "login fail...");
        client->disconnectFromHost();
        return false;
    }
}

bool Widget::create_room_back(Pack pack)
{
    QStringList list = pack.getData();
    QString create_room_id = list[0];

    //获取到创建的房间号，设置给userInterface
    user_interface->setCreateRoomId(create_room_id);

    //进入直播间
    user_interface->createRoom(client, user_name, create_room_id);

}

void Widget::join_room_back(Pack pack)
{
    QStringList list = pack.getData();
    QString owner_name = list[0];
    QString join_room_id = list[1];
    user_interface->joinRoom(client, user_name, owner_name, join_room_id);
}

void Widget::onReadyRead(){

    bool ret = false;
    while(1){
        Pack pack;

        int size = 0;
        int res = client->read((char*)&size, 4);
        if(res==0 || res==-1){break;}
        client->read(pack.data()+4, size-4);
        switch (pack.getType()) {
            case TYPE_REGIST:
                ret = regist_back(pack);
                break;
            case TYPE_LOGIN:
                ret = login_back(pack);
                if(ret == true){
                    //调用用户界面
                    user_interface = new UserInterface(client, user_name);
                    this->hide();
                    user_interface->show();
                }
                break;
             case TYPE_CREATE_ROOM:
                create_room_back(pack);
                break;
             case TYPE_JOIN_ROOM:
                join_room_back(pack);
                break;
        }
     }
}

//注册
void Widget::on_pushButton_clicked(){

    Pack pack;
    QString name = ui->lineEdit->text();
    QString pswd = ui->lineEdit_2->text();

    pack.setType(TYPE_REGIST);
    pack.append(name);
    pack.append(pswd);

    client->write(pack.data(), pack.size());
}

//登录
void Widget::on_pushButton_2_clicked()
{

    Pack pack;
    QString name = ui->lineEdit->text();
    QString pswd = ui->lineEdit_2->text();
    user_name = name;

    qDebug() << "name: " << name;
    qDebug() << "pwd: " << pswd;

    pack.setType(TYPE_LOGIN);
    pack.append(name);
    pack.append(pswd);

    client->write(pack.data(), pack.size());

    qDebug() << "send success..." ;
}
